package com.snj.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author yujie
 * @data 2024/8/22 17:48
 */
public class Demo6 {
    public static void main(String[] args) {
        List<Integer> list = dealGcClassNum(18, 5, 13);
        System.out.println(list);
    }

    public static List<Integer> dealGcClassNum(Integer classNum, Integer startNum, Integer endNum) {
        List<Integer> oneList = new ArrayList<>();

        int oneDept = classNum;

        if (oneDept <= startNum) {
            oneList.add(startNum);
        } else {
            // 计算整除结果
            int quotientOneDept = oneDept / endNum;
            if (quotientOneDept > 0) {
                for (int i = 0; i < quotientOneDept; i++) {
                    oneList.add(endNum);
                }
            }

            // 计算余数
            int remainderOneDept = oneDept % endNum;

            int oneFlag = 0;
            boolean isValidOneDept = isValidRemainder(remainderOneDept, startNum, endNum);
            if (isValidOneDept) {
                oneList.add(remainderOneDept);
            } else {
                // 余数不是 奇数
                // 判断余数是否大于5
                if (remainderOneDept > startNum) {
                    int[] ints = findTwoOddNumbers(remainderOneDept, startNum, endNum);
                    if (ints.length > 1) {
                        for (int i = 0; i < ints.length; i++) {
                            oneList.add(ints[i]);
                        }
                    } else {
                        for (int i = 0; i < ints.length; i++) {
                            oneList.add(ints[i]);
                        }
                        // 这里最后肯定留下的是 1
                        oneFlag = 1;
                    }
                } else {
                    oneFlag = remainderOneDept;
                }
            }

            // 计算总剩余
            if (oneFlag > 0) {
                if (!oneList.isEmpty()) {
                    int index = 0;
                    for (Integer i : oneList) {
                        if (i < endNum) {
                            if (i + oneFlag < endNum && oneFlag != 0) {
                                boolean validJiShu = false;
                                validJiShu = isValidJiShu(i + oneFlag);
                                if (validJiShu) {
                                    oneList.set(index, i + oneFlag);
                                } else {
                                    oneList.set(index, i + oneFlag + 1);
                                }
                                oneFlag = 0;
                                break;
                            } else {
                                // 大于 endNum
                                oneList.set(index, endNum);
                                oneFlag += i + oneFlag - endNum;
                            }
                        }
                        index += 1;
                    }
                }
            }


            if (oneFlag > 0) {
                // 如果 最后还有人员剩余, 说明 列表中的人数 都是 13
                // 找一个进行拆分
                Integer totalNum = oneList.get(0);
                boolean validJiShu = isValidJiShu(totalNum + oneFlag);

                if (validJiShu) {
                    int[] ints = findTwoOddNumbers2(totalNum + oneFlag + 1, startNum, endNum);
                    for (int i = 0; i < ints.length; i++) {
                        if (i == 0) {
                            oneList.set(0, ints[i]);
                        } else {
                            oneList.add(ints[i]);
                        }
                    }
                } else {
                    int[] ints = findTwoOddNumbers2(totalNum + oneFlag, startNum, endNum);
                    for (int i = 0; i < ints.length; i++) {
                        if (i == 0) {
                            oneList.set(0, ints[i]);
                        } else {
                            oneList.add(ints[i]);
                        }
                    }
                }

            }


        }

        Collections.sort(oneList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1); // 降序
            }
        });

        return oneList;
    }


    // 判断余数是否在 5 到 13 之间（包括5和13），并且是奇数
    private static boolean isValidRemainder(int remainder, Integer startNum, Integer endNum) {
        return remainder >= startNum && remainder <= endNum && remainder % 2 != 0;
    }

    // 判断 是否是奇数
    private static boolean isValidJiShu(int remainder) {
        return remainder % 2 != 0;
    }

    // 查找两个奇数的和等于 input 的值，或返回最接近13的奇数
    private static int[] findTwoOddNumbers(int input, Integer startNum, Integer endNum) {
        // 确保输入在5到13之间
        if (input < startNum || input > endNum) {
            throw new IllegalArgumentException("输入值必须在5到13之间");
        }

        // 奇数数组  startNum endNum
//        int[] oddNumbers = {5, 7, 9, 11, 13};
        int[] oddNumbers = getOddNumbersBetween(startNum, endNum);

        // 寻找两个奇数
        for (int i = 0; i < oddNumbers.length; i++) {
            for (int j = i; j < oddNumbers.length; j++) {
                if (oddNumbers[i] + oddNumbers[j] == input) {
                    return new int[]{oddNumbers[i], oddNumbers[j]};
                }
            }
        }

        // 如果没有找到两个奇数，则返回最接近13的奇数
        return new int[]{(input - 1)};
    }

    private static int[] findTwoOddNumbers2(int input, Integer startNum, Integer endNum) {
        // 奇数数组  endNum
//        int[] oddNumbers = {5, 7, 9, 11, 13};
        int[] oddNumbers = getOddNumbersBetween(startNum, endNum);

        // 寻找两个奇数
        for (int i = 0; i < oddNumbers.length; i++) {
            for (int j = i; j < oddNumbers.length; j++) {
                if (oddNumbers[i] + oddNumbers[j] == input) {
                    return new int[]{oddNumbers[i], oddNumbers[j]};
                }
            }
        }

        // 如果没有找到两个奇数，则返回最接近13的奇数
        return new int[]{(input - 1)};
    }

    public static int[] getOddNumbersBetween(int startNum, int endNum) {
        // 确保 startNum <= endNum
        if (startNum > endNum) {
            int temp = startNum;
            startNum = endNum;
            endNum = temp;
        }

        List<Integer> oddList = new ArrayList<>();

        // 遍历范围
        for (int i = startNum; i <= endNum; i++) {
            if (i % 2 != 0) {  // 判断奇数
                oddList.add(i);
            }
        }

        // 将 List 转换为数组
        int[] oddArray = new int[oddList.size()];
        for (int i = 0; i < oddList.size(); i++) {
            oddArray[i] = oddList.get(i);
        }

        return oddArray;
    }
}
